package com.bigdata.movieticketapi.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bigdata.movieticketapi.common.exception.BusinessException;
import com.bigdata.movieticketapi.common.exception.ResponseCode;
import com.bigdata.movieticketapi.common.pojo.PageResult;
import com.bigdata.movieticketapi.entity.MovieType;
import com.bigdata.movieticketapi.mapper.MovieTypeMapper;
import com.bigdata.movieticketapi.service.IMovieTypeService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bigdata.movieticketapi.vo.req.MovieTypeReqVO;
import com.bigdata.movieticketapi.vo.req.PageMovieTypeReqVO;
import com.bigdata.movieticketapi.vo.resp.PageMovieTypeRespVO;
import com.bigdata.movieticketapi.vo.resp.SelectListRespVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author movie
 * @since 2023-11-07
 */
@Service
public class MovieTypeServiceImpl extends ServiceImpl<MovieTypeMapper, MovieType> implements IMovieTypeService {

    @Resource
    private MovieTypeMapper movieTypeMapper;

    @Override
    public void addMovieType(MovieTypeReqVO vo) {
        MovieType movieType = query().select("id").last("limit 1").eq("type",vo.getType()).one();
        if(movieType !=null){
            throw new BusinessException(ResponseCode.MOVIE_TYPE_ALREADY_EXISTS.getCode(), ResponseCode.MOVIE_TYPE_ALREADY_EXISTS.getMessage());
        }
        MovieType insertMovieType = new MovieType();
        BeanUtils.copyProperties(vo,insertMovieType);
        int result = movieTypeMapper.insert(insertMovieType);
        if(result != 1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public void updateMovieType(MovieType movieType) {
        int result = movieTypeMapper.updateById(movieType);
        if (result !=1){
            throw new BusinessException(ResponseCode.OPERATION_ERROR.getCode(),ResponseCode.OPERATION_ERROR.getMessage());
        }
    }

    @Override
    public PageResult<PageMovieTypeRespVO> pageMovieTypes(PageMovieTypeReqVO vo) {
        IPage<PageMovieTypeRespVO> result = movieTypeMapper.selectPageMovieType(new Page<>(vo.getCurrent(), vo.getSize()), vo);
        return PageResult.getPage(result.getRecords(), result.getTotal());
    }

    @Override
    public List<SelectListRespVO> getMovieTypeSelectList() {
        return movieTypeMapper.selectMovieTypeSelectList();
    }
}
